package ee.example.final_work

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.CursorFactory
import android.database.sqlite.SQLiteOpenHelper

class DataBaseHelper(context: Context?, name: String?, factory: CursorFactory?, version: Int) :
    SQLiteOpenHelper(context, name, factory, version) {
    override fun onCreate(sqLiteDatabase: SQLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_USERS)
        sqLiteDatabase.execSQL("INSERT INTO users (name,password) VALUES ('admin','123456')")
    }

    override fun onUpgrade(sqLiteDatabase: SQLiteDatabase, i: Int, i1: Int) {}

    companion object {
        private var instance: DataBaseHelper? = null
        private const val CREATE_USERS = ("create table users"
                + "(id integer primary key autoincrement,"
                + "name text,"
                + "password text)")

        @JvmStatic
        fun getInstance(context: Context?): DataBaseHelper? {
            if (instance == null) instance = DataBaseHelper(context, "travel.db", null, 2)
            return instance
        }
    }
}